<script setup>
import * as echarts from 'echarts'
import { effect, onMounted, ref, watch } from 'vue'

const props = defineProps({
  msg: String,
  data: {
    type: Object,
    required: true
  }
})
console.log('props=>data', props.data)
// 1.初始化echarts实例
let myChart = null
const target = ref(null)

onMounted(() => {
  myChart = echarts.init(target.value)
  renderChart()
})

// 2.构建option配置对象
const renderChart = () => {
  const options = {
    // X轴展示数据
    xAxis: {
      show: false, // 是否显示X轴
      type: 'value', // X轴是否作为数据进行展示
    },
    // Y轴展示数据
    yAxis: {
      show: false, // 是否显示Y轴
      type: 'value', // Y轴是否作为数据进行展示
    },
    // 图表绘制的位置，对应上下左右
    // grid: {
    //   top: 0,
    //   right: 0,
    //   bottom: 0,
    //   left: 0,
    //   containLabel: true
    // },
    // 核心配置
    series: 
    [
      {
        type: 'graph', // 图表类型
        layout: 'none',
        coordinateSystem: 'cartesian2d',
        symbolSize: 26,
        z: 3,
        edgeLabel: {
          normal: {
            show: true,
            color: '#fff',
            textStyle: {
              fontSize: 14
            },
            formatter: function (params) {
              return params.data.speed
            }
          }
        },
        label: {
          normal: {
            show: true,
            position: 'bottom',
            color: '#5e5e5e'
          }
        },
        edgeSymbol: ['none', 'arrow'],
        edgeSymbolSize: 8,
        data: props.data.map(item => {
          if (item.id != 0) {
            return {
              name: item.name,
              category: 0,
              active: true,
              speed: `${item.speed}kb/s`,
              value: item.value,
              itemStyle: {
                normal: {
                  color: {
                    colorStops: [
                      {
                        offset: 0, color: '#157eff'
                      },
                      {
                        offset: 1, color: '#157eff'
                      }
                    ]
                  }
                }
              },
              label: {
                normal: {
                  fontSize: 12,
                  color: '#fff',
                }
              }
            }
          } else {
            return {
              name: item.name,
              value: item.value,
              symbolSize: 100,
              itemStyle: {
                normal: {
                  color: {
                    colorStops: [
                      {
                        offset: 0, color: '#157eff'
                      },
                      {
                        offset: 1, color: '#35c2ff'
                      }
                    ]
                  }
                }
              },
              label: {
                normal: {
                  fontSize: 14,
                  color: '#fff',
                }
              }
            }
          }
        }),
        links: props.data.map((item, index) => ({
          source: item.source,
          target: item.target,  
          speed: `${item.speed}kb/s`,
          lineStyle: {
            normal: {
              color: '#12b5d0',
              curveness: 0.2
            }
          },
          label: {
            show: true,
            backgroundColor: 'transparent', // 'rgba(0,23,11,0)', // 'transparent',
            position: 'middle',
            offset: [10, 0]
          }
        }))
      },
      {
        type: 'lines',
        coordinateSystem: 'cartesian2d',
        z: 1,
        effect: {
          show: true,
          smooth: true,
          trailLength: 0.05,
          symbol: 'arrow',
          color: '#fff', // 'rgba(55,155,255,0.5 )',
          symbolSize: 3
        },
        lineStyle: {
          normal: {
            curveness: 0.2
          }
        },
        data: [
          [{coord: [0, 300]},{coord: [50, 200]}],
          [{coord: [0, 100]},{coord: [50, 200]}],
          [{coord: [50, 200]},{coord: [100, 100]}],
          [{coord: [50, 200]},{coord: [100, 300]}]
        ]
      }
    ]
  }
  // 3. 通过实例setOptions(options)
  myChart.setOption(options)
}

watch(
  () => props.data,
  () => {
    renderChart()
  }
)
</script>

<template>
  <div>
    <div>【数据传递信息】</div>
    <!-- bg-[#38bdf8]" -->
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>

<style scoped>
.read-the-docs {
  color: #888;
}
</style>
